package otherapi.itext;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 1/13/2023 2:22 PM
 */
public class PdfTest {

    public static final String TEST_1_PDF = "test1.pdf";

    @Test
    public void test1() throws DocumentException, IOException {
        String sourceFile = "test.pdf";
        String targetFile = TEST_1_PDF;
        PdfUtils.addWatermark(sourceFile, targetFile, "测试水印添加");
    }

    @Test
    public void test2() throws IOException, DocumentException {
        String sourceFile = "test.pdf";
        String targetFile = TEST_1_PDF;
        PdfReader pdfReader1 = new PdfReader(sourceFile);
        PdfStamper pdfStamper1 = new PdfStamper(pdfReader1, new FileOutputStream(targetFile), '1', true);
        PdfUtils.fillUpWaterMark(pdfStamper1, "绝密文件，禁止外传！");
        pdfStamper1.close();
    }

    @Test
    public void test3() throws Exception {
        // 创建文件
        Document document = new Document();
        // 建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TEST_1_PDF));
        //用户密码
        String userPassword = "123456";
        //拥有者密码
        String ownerPassword = "hd";
        writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
        // 打开文件
        document.open();
        //添加内容
        document.add(new Paragraph("password !!!!"));
        // 关闭文档
        document.close();
        // 关闭书写器
        writer.close();
    }
}
